<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%@ include file="../bui_header.jsp" %>

<body>

<div class="container">
    <div class="row">
      <form id="searchForm" class="form-horizontal span24">
        
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">公司名称：</label>
            <div class="controls">
              <input type="text" class="text" name="company_name">
            </div>
          </div>
          <div class="span4">
            <button type="button" id="btnSearch" class="button button-primary">搜索</button>
          </div>
        </div>
      </form>
    </div>
    <div class="search-grid-container">
      <div id="grid"></div>
    </div>

  </div>
    
  <div id="content" class="hide" disabled='true'>
      <form id="user_form" class="form-horizontal">
      	<div id="selectCompanyDiv" class="row">
	      		<div class="control-group span8">
				    <label class="control-label">请选择公司</label>
				    <div class="controls">
				    	<div id="companySelect"></div>
					    <!-- <select name="companyNameSelect"></select> -->
				    </div>
			  	</div>
			</div>
        <div class="row">
          <div class="control-group span8" style="display:none">
            <label class="control-label"><s>*</s>编号</label>
            <div class="controls">
              <input name="number" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><s>*</s>企业名称</label>
            <div class="controls">
              <input name="companyName" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">企业收入（万元）</label>
            <div class="controls">
              <input name="incom" type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">企业收入评分</label>
            <div class="controls">
              <input name="incomGrade" type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">企业收入增长率</label>
            <div class="controls">
              <input name=incomGrowthRate type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">企业收入增长率评分</label>
            <div class="controls">
              <input name="incomGrowthRateGrade" type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">利润（万元）</label>
            <div class="controls">
              <input name="profit" type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">利润评分</label>
            <div class="controls">
              <input name="prifitGrade" type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">利润增长率</label>
            <div class="controls">
              <input name="profitGrowthRate" type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">利润增长率评分</label>
            <div class="controls">
              <input name="profitGrowthRateGrade" type="text" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">年度</label>
            <div class="controls">
              <input name="belongYear" type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
      </form>
    </div>
    
    <div class="container hide">
	    <div class="row">
	    <div class="span24">
	      <div id="importFileDiv">
	      </div>
	    </div>
	 </div>
	 </div>
  
<script type="text/javascript">
BUI.use(['common/search', 'bui/overlay', 'bui/uploader', 'bui/form', 'bui/select', 'bui/data'], 
		function (Search, Overlay, Uploader, Form, Select, Data) {
    
	 var editing = new BUI.Grid.Plugins.DialogEditing({
	        contentId : 'content', //设置隐藏的Dialog内容
	        autoSave : true,//添加数据或者修改数据时，自动保存
	      });
    
    var columns = [
        /* {title:'编号',dataIndex:'number',width:'10%'
        }, */
        {title:'企业名称', dataIndex:'companyName', width:'20%'},
        {title:'所属年度', dataIndex:'belongYear', width:'8%'},
        {title:'企业收入（万元）', dataIndex:'incom',width:'12%',
        	renderer: function(value, obj) {
        		return value + '（￥）';
        	}},
        {title:'企业收入评分',dataIndex:'incomGrade',width:'10%'},
        {title:'企业收入增长率',dataIndex:'incomGrowthRate',width:'10%'}, 
        {title:'企业收入增长率评分',dataIndex:'incomGrowthRateGrade',width:'13%'}, 
        {title:'利润（万元）',dataIndex:'profit',width:'10%',
        	renderer: function(value, obj) {
        		return value + '（￥）';
        	}}, 
        {title:'利润评分',dataIndex:'prifitGrade',width:'8%'},
        {title:'利润增长率',dataIndex:'profitGrowthRate',width:'10%'}, 
        {title:'利润增长率评分',dataIndex:'profitGrowthRateGrade',width:'10%'},
        {title: '操作', width: '16%', renderer: function(value, obj) {
      	  var viewBtn = '<span class="grid-command btn-view">查看</span>&nbsp;&nbsp;';
      	  var editBtn = '<span class="grid-command btn-edit">编辑</span>';
      	  return viewBtn + editBtn;
        }}
      ];
    var store = Search.createStore('<%=contextPath %>/bizinfo/yzgs/getdata.do', {
  	  autoSync: true,
  	  proxy: {
    		method: "post",
    		dataType : 'json',
    		save : { //也可以是一个字符串，那么增删改，都会往那么路径提交数据，同时附加参数saveType
				addUrl : '<%=contextPath %>/bizinfo/yzgs/editData.do?action=add',
				updateUrl : '<%=contextPath %>/bizinfo/yzgs/editData.do?action=update',
				removeUrl : '<%=contextPath %>/bizinfo/yzgs/editData.do?action=remove'
       	}
  	  }
    });
    initSearchStore(store);
    
    var height = $(window).height() - 70;  
    var gridCfg = Search.createGridCfg(columns, {
    	height: height,
  	  //width:'200%',
      tbar : {
        items : [
          {text : '<i class="icon-plus"></i>新建',btnCls : 'button button-small',handler: addFunction},
          {text : '<i class="icon-remove"></i>删除',btnCls : 'button button-small',handler : delFunction},
          {text : '<i class="icon-upload icon-white"></i>导入',btnCls : 'button button-small button-primary', handler: importData}
        ]
      },
      plugins : [BUI.Grid.Plugins.CheckSelection,editing] // 插件形式引入多选表格
    });

  var search = new Search({
      store : store,
      gridCfg : gridCfg
    });
  var grid = search.get('grid');
  
  initEditing(editing);
  
  ss();
  
  function ss() {
  	var $grid = $(".bui-grid");
  	if (!$grid)
		  return ;
  	var $gridBody = $grid.find(".bui-grid-body");
  	if (!$gridBody)
		  return ;
  	$gridBody.css('overflow-y', 'auto');
	  
	  var $gridTable = $grid.find(".bui-grid-table");
	  if ($gridTable)
		  $gridTable.width($gridBody.width()); 
  }
  // 新增操作
  function addFunction(){
      var newData = {isNew : true}; //标志是新增加的记录
      editing.add(newData); //添加记录后，直接编辑
      setCompanySelectVisible(true);
  }
  
  // 删除操作
  function delFunction(){
    var selections = grid.getSelection();
    delItems(selections);
  }
  // 导入操作
 function importData() {
  	var uploader = new Uploader.Uploader({
 	      	render: '#importFileDiv',
 	      	url: '<%=contextPath %>/bizinfo/yzgs/importData.do',
 	      	autoUpload: false,
 	      	multiple: false,
 	      	text: '选择导入文件',
 	     	width: 360,
 	     	rules: {
 	     	 // maxSize: [1024, '文件最大不能超过1M!'],
 	     	 // minSize: [1, '文件最小不能小于1k!'],
 	     	  max: [1, '文件最多不能超过{0}个！'],
 	     	 // min: [1, '文件最少不能少于{0}个!'],
 	     	  ext: ['.xls', '文件类型只能为{0}']
 	     	},
 	     	isSuccess: function(result) {
 	     		if (result && result.success) {
		   	     	BUI.Message.Alert('导入数据成功！', function(){
		   	     		importDialog.close();
		   	     		search.load();
		   	        }, 'success');
 	     		} else {
 	     			BUI.Message.Alert(result.error, function(){
		   	          //alert('确认');
		   	        }, 'error');
 	     		}
 	     	}
 	    }).render();
  	 
  	var importDialog = new Overlay.Dialog({
          title: '导入文件',
          width: 400,
          height: 200,
          contentId: 'importFileDiv',
          success:function () {
            //alert('确认');
            //this.close();
            uploader.upload();
          }
        });
  	importDialog.show();
  }

  function delItems(items){
    var ids = [];
    BUI.each(items,function(item){
      ids.push(item.id);
    });

    if(ids.length){
      BUI.Message.Confirm('确认要删除选中的记录么？',function(){
      	$.post('<%=contextPath %>/bizinfo/yzgs/editData.do?action=remove', {ids: ids}, function(ret) {
      		if(ret.success){ //删除成功
              	search.load();
              	BUI.Message.Alert('删除成功！');
              } else { //删除失败
                  BUI.Message.Alert('删除失败！');
              }
      	}, 'json');
      },'question');
    }
  }

  //监听事件，删除一条记录
  grid.on('cellclick', function(ev) {
  	var sender = $(ev.domTarget);
     var editor = editing.get('editor');
     var contain = false;
		if(sender.hasClass('btn-edit') && editor){
			contain = true;
			editing.edit(ev.record);
			setCompanySelectVisible(true);
		}
     if (sender.hasClass('btn-view') && editor) {
     	contain = true;
   		var record = ev.record;
   		var view = {"view": true};
   		record.trigger_action = "view";
   		editing.showEditor(record);
   		setEditingEnable(editing, false);
   		setCompanySelectVisible(false);
   	}
     if (contain && grid.get('multipleSelect')){
		    return false;
		}
 	});
  
	//select company 
  var selectStore = new Data.Store({
      url : '<%=contextPath %>/bizinfo/yzgs/baseCompanyList.do',
      autoLoad : true,
      //root: "rows",
      proxy: {
    		method: "post",
    		dataType : 'json'
      }
  });
    var select = new Select.Suggest({  
      render:'#companySelect',
      //valueField:'#hide',
      multipleSelect : false,
      name: 'company_name',
      store : selectStore,
      dataType : 'json',
      cacheable: true,
      url: '<%=contextPath %>/bizinfo/yzgs/selectCompany.do',
    });
    select.render();
    select.on('change', function(ev){
      //alert(ev.item.text);
      $("input[name='number']").val(ev.item.value);
      $("input[name='companyName']").val(ev.item.text);
    });
    
    function setCompanySelectVisible(visible) {
  	  var $select = $("#selectCompanyDiv");
  	  if (visible) {
  		  $select.show();
  		  $select.find("input[name='company_name']").val();
  	  } else {
  		  $select.hide();
  	  }
    }
});

</script>

		
</body>
</html>